﻿@using OA.AspNetCore.Html;
@using OA.Model;
@using OA.Core.Model;
@using System.Text
@model OA.Model.DB.TableInfo

@{
    Layout = null;
    List<ExtendField> list = OA.Core.Helper.JsonHelper.DeserializeList<ExtendField>(Model.ConfigContent);
    var parents = list.Where(m => !string.IsNullOrWhiteSpace(m.ParentName)).ToList();
    var myGroups = parents.GroupBy(m => m.ParentName).ToList();
    string jsContent = string.Empty;
    List<Dictionary<string, string>> infoTableValues = new List<Dictionary<string, string>>() { new Dictionary<string, string>() };
}
<table class="layui-table" style="width:100%;">
    <thead>
        @if (list != null)
        {
            if (myGroups.Count != parents.Count)
            {
                <tr>
                    @foreach (var group in myGroups)
                    {
                        int children = group.Count();
                        if (children == 1)
                        {
                            <td rowspan="2" style="text-align:center;">@group.Key</td>
                        }
                        else
                        {
                            <td colspan="@children" style="text-align:center;">@group.Key</td>
                        }
                    }
                    <td rowspan="2" style="text-align:center;">#</td>
                </tr>
            }
        }
        <tr>
            @foreach (var item in list)
            {


                if (myGroups.Count == parents.Count || item.ParentName != item.Name)
                {
                    <th> @item.Name </th>
                }

                if (item.Type == 69 || item.Type == 68)
                {
                    if (!string.IsNullOrWhiteSpace(item.Values))
                    {
                        string[] myFields = item.Values.Split(new string[] { "+", "-", "*", "/", "(", ")", "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
                        List<string> fields = new List<string>();
                        string objs = string.Empty;
                        foreach (string key in myFields)
                        {
                            double temp = 0;
                            if (!double.TryParse(key, out temp))
                            {
                                fields.Add("." + key);
                                objs += "var " + key + "=parseFloat(trObj.find('." + key + "').val());\r\n";
                            }
                        }
                        if (fields.Count > 0)
                        {
                            string objTotal = string.Join(",", fields);
                            objs += "trObj.find('." + item.Attribute + "').val(Math.formatFloat(" + item.Values + ",2));\r\n";
                            jsContent += "<script type='text/javascript'>$(document).on('change', '" + objTotal + "', function(){\r\nvar trObj = $(this).parent().parent();\r\n" + objs + "});</script>";

                        }
                    }
                }
                else if (item.Type == 3 && !string.IsNullOrWhiteSpace(item.Values) && (item.Values.StartsWith("http://") || item.Values.StartsWith("https://")))
                {
                    try
                    {
                        System.Net.WebClient wc = new System.Net.WebClient();
                        wc.Encoding = Encoding.UTF8;
                        string result = wc.DownloadString(item.Values);
                        StringBuilder sb = new StringBuilder();
                        sb.Append(@"<script type=""text/javascript"">
                        var " + item.Attribute + @" = " + result + @";
                        layui.use('form', function () {
                            var form = layui.form;
                            form.on('select(" + item.Attribute + @")', function (data) {
                                var trInfo=$(data.elem).parent().parent();
                                if($(data.elem).parent().hasClass(""layui-input-block""))
                                {

                                }
                                $.each(" + item.Attribute + @", function (index, info) {
                                    if (info.Value == data.value) {
                                        for (var key in info) {
                                            if (trInfo.find("".""" + @"+key).length > 0) { trInfo.find("".""" + @"+key).val(info[key]); }
                                        }
                                    }
                                });
                            });
                        });
                    </script>");
                        jsContent = sb.ToString();
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
            @if (myGroups.Count == parents.Count)
            {
                <td style="text-align:center;">#</td>
            }
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < infoTableValues.Count; i++)
        {
            var valueItem = infoTableValues[i];

            <tr data-id="@i">
                @for (int j = 0; j < list.Count; j++)
                {
                    ExtendField infoExtendField = new ExtendField();
                    infoExtendField = list[j];
                    var cellValue = string.Empty;
                    if (valueItem.ContainsKey(infoExtendField.Attribute))
                    {
                        infoExtendField.Value = valueItem[infoExtendField.Attribute];
                    }
                    else
                    {
                        infoExtendField.Value = infoExtendField.DefaultValue;
                    }
                    <td>
                        @Html.Raw(MvcHtmlExtension.GetMyEditorControl(infoExtendField, "tableValues.RowValues[" + i.ToString() + "].CellValues."))
                    </td>
                }
                <td>
                    <input type="hidden" class="Status" value="1" name="tableValues.RowValues[@i].Status" />
                    <span class="layui-btn layui-btn-xs layui-btn-danger deleteItem">删除</span>
                </td>
            </tr>
        }
    </tbody>
    <tfoot>
        @{
            int colspan = list.Count + 1;
        }
        <tr><td colspan="@colspan" class="right_content"><button type="button" class="layui-btn layui-btn-normal addItem">增加一行</button></td></tr>
    </tfoot>
</table>